package com.yatush.prepare.jsp;

import java.util.List;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.yatush.prepare.jsp.markers.FormAble;

public class JspTable implements FormAble{

	private Element table;
	
	public JspTable(TableHelper tableData) {
		table =  DocumentHelper.createElement("table");
		table.addNamespace("c", "http://java.sun.com/jsp/jstl/core");
		Element headers = table.addElement("tr");
		for(ColumnHelper col : tableData.columns){
			headers.addElement("th").addText(col.getColumnLabel());
		}
		Element forEach = table.addElement("c:forEach");
		forEach.addAttribute("var","row");
		forEach.addAttribute("items","${"+tableData.getListName()+"}");
		Element dataRows = forEach.addElement("tr");
		for(ColumnHelper col : tableData.columns){
			dataRows.addElement("td").addElement("c:out").addAttribute("value", "${row."+col.getColumnSourceName()+"}");
		}
	}
	
	
	
	@Override
	public Element getDomElement() {
		return table;
	}
	
	public static class TableHelper{
		private String listName;
		private List<ColumnHelper> columns;
		
		public String getListName() {
			return listName;
		}
		public void setListName(String listName) {
			this.listName = listName;
		}
		public List<ColumnHelper> getColumns() {
			return columns;
		}
		public void setColumns(List<ColumnHelper> columns) {
			this.columns = columns;
		}
	}
	
	public static class ColumnHelper{
		private String columnSourceName;
		private ColumnType columnType;
		private String columnLabel; 
				
		public ColumnHelper(String columnSourceName, ColumnType columnType,
				String columnLabel) {
			this.columnSourceName = columnSourceName;
			this.columnType = columnType;
			this.columnLabel = columnLabel;
		}
		
		public String getColumnSourceName() {
			return columnSourceName;
		}
		public void setColumnSourceName(String columnSourceName) {
			this.columnSourceName = columnSourceName;
		}
		public ColumnType getColumnType() {
			return columnType;
		}
		public void setColumnType(ColumnType columnType) {
			this.columnType = columnType;
		}
		public String getColumnLabel() {
			return columnLabel;
		}
		public void setColumnLabel(String columnLabel) {
			this.columnLabel = columnLabel;
		}
	}
	
	public enum ColumnType{
		TEXT,NUMBER,DATE;
	}

}
